﻿using AutoMapper;
using MediatR;
using TraceabilityApp_Api.Application.Command.Breed_养殖_;
using TraceabilityApp_Domain;
using TraceabilityApp_ErrorCode;
using TraceabilityApp_Infrastructure;

namespace TraceabilityApp_Api.Application.Handler.Breed_养殖_
{
    public class UpdateBreedHandler : IRequestHandler<UpdateBreedCommand, ApiResult<int>>
    {
        private readonly IBase<BreedModel> breedRepo;
        private readonly IMapper mapper;

        public UpdateBreedHandler(IBase<BreedModel> breedRepo,IMapper mapper)
        {
            this.breedRepo = breedRepo;
            this.mapper = mapper;
        }
        public Task<ApiResult<int>> Handle(UpdateBreedCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result=new ApiResult<int>();
            var breed = breedRepo.GetValue(request.BreedId).Result;
             breed =mapper.Map(request,breed);
            var r=breedRepo.Update(breed).Result;
            if(r>0)
            {
                result.Code = ApiEnum.OK;
                result.Data= r;
                result.Msg = "养殖信息修改成功";
                return Task.FromResult(result);
            }
            else
            {
                result.Code = ApiEnum.Fail;
                result.Data = r;
                result.Msg = "养殖信息修改失败";
                return Task.FromResult(result);
            }
        }
    }
}
